package power

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type PowerGetRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPowerGetRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PowerGetRoleLogic {
	return &PowerGetRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PowerGetRoleLogic) PowerGetRole(req *types.PowerGetRoleReq) (resp *types.PowerGetRoleResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.PowerGetRole(ctx, &usercenter.PowerGetRoleReq{
		Id: req.PowerId,
	})
	var roleList []types.Role
	if len(res.RoleList) > 0 {
		for _, role := range res.RoleList {
			roleList = append(roleList, types.Role{
				Id:       role.Id,
				Name:     role.Name,
				ParentId: role.ParentId,
				Weight:   role.Weight,
				CreateAt: role.CreateAt,
				UpdateAt: role.UpdateAt,
			})
		}
	}
	resp = new(types.PowerGetRoleResp)
	resp.RoleList = roleList
	return
}
